/*
 * File:   mainForm.cpp
 * Author: hao
 *
 * Created on 2011年5月4日, 上午2:15
 */

#include <qt4/QtGui/qdialog.h>
#include <qt4/QtGui/QFileDialog>
#include <qt4/QtGui/QMessageBox>
#include <qt4/QtCore/QTextCodec>

#include <string>


#include "mainForm.h"
#include "aboutUSForm.h"

mainForm * mainForm::pSelf;

mainForm::mainForm() {
    pSelf = this;
    widget.setupUi(this);

    QTextCodec *codec = QTextCodec::codecForName("utf8");

    QTextCodec::setCodecForLocale(codec);

    QTextCodec::setCodecForCStrings(codec);

    QTextCodec::setCodecForTr(codec);

    timer = new QTimer(this);

    connect(widget.AboutUs, SIGNAL(clicked()), this, SLOT(onAboutUS()));
    connect(widget.addFile, SIGNAL(clicked()), this, SLOT(onAddFile()));
    connect(widget.Play, SIGNAL(clicked()), this, SLOT(onPlay()));
    connect(widget.Stop, SIGNAL(clicked()), this, SLOT(onStop()));
    connect(widget.Pause, SIGNAL(clicked()), this, SLOT(onPause()));
    connect(timer, SIGNAL(timeout()), this, SLOT(onTick()));
    connect(widget.VolumeControler, SIGNAL(valueChanged(int)), this, SLOT(onDialChange(int)));

    pPane = new Pane();

    pPane->createCallback(0);
    pPane->setPalyEndCallback(playendcallback);

    timer->start(1000);
    this->widget.VolumeShow->display( 1 );
    widget.VolumeControler->setValue( 180 );
    

}

void mainForm::onAboutUS() {
    aboutUSForm aboutUS;
    aboutUS.show();
    aboutUS.exec();
}

void mainForm::onTick() {
    if (!pPane->isSetup()) {
        widget.playingBar->setValue(0);
        return;

    }

    char valueStr[100];

    unsigned long long time = (unsigned long long) pPane->getCurrentTime();

    sprintf(valueStr, "%d:%d/%d:%d", (int) (time / 60), int( time % 60), min, second);

    double tmp = (double) time / (min * 60 + second);

    widget.playingBar->setValue((int) (tmp * 100));

    widget.TimeLabel->setText(tr(valueStr));

}

void mainForm::onPlayEnd() {
    widget.palyList->setCurrentRow(widget.palyList->currentRow() + 1);
    widget.Play->click();
}

void mainForm::onPlay() {

    static int lastSelect = -1;

    int index = widget.palyList->currentRow();

    if (index == -1) return;

    if (index != lastSelect) {
        if (lastSelect != -1)
            pPane->Stop();
        lastSelect = index;
    }

    if (pPane->isSetup()) {
        pPane->Resume();
    } else {

        try {

            if (vplayListEntry[index].getType() == playListEntry::FILETYPE)
                pPane->PlayingFile(vplayListEntry[index].getPath()->toUtf8().data(), &min, &second);
            else
                pPane->PlayingTrack(vSheet[vplayListEntry[index].getTrackSheetIndex()], vplayListEntry[index].getTrackIndex(), &min, &second);
            pPane->Resume();
            pPane->setVolumeLinear( this->widget.VolumeShow->value() );

        } catch (const Error & ex) {
            
            pPane->Stop();

            QMessageBox::information(this, "Note", tr(ex.what()));

        }

    }

}

void mainForm::onDialChange(int value){
    
    widget.VolumeShow->display( 2 * value / 360.0 );
    pPane->setVolumeLinear( widget.VolumeShow->value() );
    
}

void mainForm::onStop() {
    pPane->Stop();
}

void mainForm::onPause() {
    pPane->Pause();
}

void mainForm::onAddFile() {

    string fileFilter = "Surpor Format( *.cue ";

    fileFilter += pPane->getSurportPrefixName();

    fileFilter += ")";

    QStringList pathList = QFileDialog::getOpenFileNames(this, tr("Open Sound"), NULL, tr(fileFilter.c_str()));

    for (int i = 0; i < pathList.count(); i++) {

        string temp = pathList.at(i).toStdString();


        if (pathList.at(i).count() != 0) {

            if (temp.substr(temp.size() - 4, 4) == ".cue") {

                vSheet.push_back(pPane->AnalysisCue(pathList.at(i).toUtf8().data()));

                unsigned index = vSheet.size() - 1;


                for (unsigned i = 0; i < vSheet[index].TrackList.size(); i++) {

                    vplayListEntry.push_back(playListEntry(index, i));

                    QString tmp;

                    char buffer[255];
                    pPane->strRecoding(vSheet[index].Title.c_str(), buffer, 255);
                    tmp += buffer;
                    tmp += "\t\t";

                    pPane->strRecoding(vSheet[index].TrackList[i].Title.c_str(), buffer, 255);
                    tmp += buffer;

                    widget.palyList->addItem(tmp);

                }


            } else {

                vplayListEntry.push_back(playListEntry(pathList.at(i)));

                widget.palyList->addItem(pathList.at(i));
            }

        }
    }


}

mainForm::~mainForm() {

    delete timer;

    delete pPane;

}
